package com.wwy.producter;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;
import com.wwy.config.RabbitMqUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Objects;

/**
 * @author 王伟羽
 * @date 2024/3/13 13:53
 */
@RestController
@RequestMapping(value = "/workQueues")
public class WorkQueuesProducer {

    private final static String WORK_QUEUES = "work_queues";

    @Resource
    private RabbitTemplate rabbitTemplate;



    @GetMapping(value = "/workQueuesSendMessage")
    public String workQueuesSendMessage(String message) {
        System.out.println("接收消息");
        //第一种生产消息方法
        //获取连接
        Connection connection = RabbitMqUtils.getConnection();
        if (Objects.nonNull(connection)) {
            try {
                Channel channel = connection.createChannel();
                //发送消息
                for (int i = 0; i < 10; i++) {
                    channel.basicPublish("", WORK_QUEUES, null, (message + i).getBytes("utf-8"));
                }
                return "发送成功！";
            } catch (IOException e) {
                e.printStackTrace();
                return "发送失败！";
            }
        }
        return "初始mq失败！";
    }

    @GetMapping(value = "/workQueuesSendMessage01")
    public String workQueuesSendMessage01(String message) {
        System.out.println("接收消息");
        //发送消息
        for (int i = 0; i < 10; i++) {
            System.out.println(i);
            rabbitTemplate.convertAndSend(WORK_QUEUES, message);
        }
        return "发送成功！";
    }


}
